const router = require('koa-router')()
const { SucceedMess, ErrMess } = require('../res-message/index')
const { userVerify } = require('../middleware/loginCheck')
const { OrderDb, orderList } = require('../controller/order')


router.prefix('/api/order')

// 创建订单
router.post('/', userVerify, async (ctx, next) => {
    const data = ctx.request.body
    const { username } = ctx.session.userInfo

    try{
        const orderData = await OrderDb(data, username)
        ctx.body = new SucceedMess(orderData)
    }catch(e){
        console.error(e)
        ctx.body = new ErrMess(10007, '创建出错')
    }
})


// 获取订单列表
router.get('/', userVerify, async (ctx, next) => {
    const list = await orderList()

    ctx.body = new SucceedMess(list)
})






module.exports = router